//
//  TKYRemarkImageView.m
//  ProgressReportProject
//
//  Created by apple on 2018/4/16.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYRemarkImageView.h"
#import "TKYRemarkImageCell.h"

@implementation TKYRemarkImageView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
        
    }
    return self;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.imageArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TKYRemarkImageCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:KRemarkImageCellID forIndexPath:indexPath];
    
    //[cell.deletedBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
//    UIImage *image = self.imageArr[indexPath.row];
//    NSData *imageData = [[NSData alloc]initWithBase64EncodedString:self.imageArr[indexPath.row] options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
    // cell.imagePicV.image = [UIImage imageWithData:imageData];
    cell.imagePicV.image = self.imageArr[indexPath.row];
    cell.deleteImageBlock = ^(UIButton *deleteImageButton) {
        //从资源数组中移除该数据
        [self.imageArr removeObject:self.imageArr[indexPath.row]];
        [self.collectionView reloadData];
        if (self.imageArr.count == 0) {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"imageArrCount" object:nil];
        }
    };
    return cell;
}


#pragma mark--懒加载
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(kWidthScale *160, kHeightScale *160);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 2;
        //layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 20);
        UICollectionView *collectionView;
//        if (IS_iPhoneX) {
//            collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 100) collectionViewLayout:layout];
//        }
        collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeightScale *160) collectionViewLayout:layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = kMainColour;
        collectionView.bounces = YES;
        //注册cell
        [collectionView registerNib:[UINib nibWithNibName:@"TKYRemarkImageCell" bundle:nil] forCellWithReuseIdentifier:KRemarkImageCellID];
        collectionView.scrollsToTop = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        if (self.seletetedCollectionCellBlock) {
            self.seletetedCollectionCellBlock(indexPath);
        }
}

/**
 移除通知
 */
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
